package com.fly.feishu.web.controller;

import com.alibaba.fastjson.JSONObject;
import com.fly.feishu.api.dto.WebAccessToken;
import com.fly.feishu.api.service.FeishuTokenService;
import com.fly.feishu.infrastructure.persistent.po.Employee;
import com.fly.feishu.web.helper.FeishuUserContext;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.servlet.ModelAndView;

@Controller
@RequestMapping("/")
public class IndexContreller {

    @Autowired
    private FeishuTokenService feishuTokenService;

    @RequestMapping("/")
    public ModelAndView indexPage() {
        ModelAndView model = new ModelAndView("index");
        model.setViewName("index");
        return model;
    }

    /**
     * 用户信息
     *
     * @return
     */
    @RequestMapping("/profile")
    public ModelAndView profile() {
        ModelAndView model = new ModelAndView("profile");
        Employee currentToken = FeishuUserContext.getCurrentToken();
        JSONObject obj = new JSONObject();
        obj.put("name", currentToken.getName());
        obj.put("enName", currentToken.getName());
        obj.put("email", currentToken.getEmail());
        obj.put("mobile", currentToken.getMobile());
        WebAccessToken feishuToke = FeishuUserContext.getCurrentWebToke();
        if (feishuToke != null) {
            obj.put("avatarUrl", feishuToke.getAvatarUrl());
            obj.put("openId", feishuToke.getOpenId());
            obj.put("enName", feishuToke.getEnName());
        }
        model.addObject("info", obj);
        return model;
    }

    /**
     * 页面
     */
    public void pageForward() {

    }
}
